package org.swan.web.configuration;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.stream.tool.db.DataPool;
import org.stream.tool.db.EnvSource;

import javax.sql.DataSource;
import java.util.Map;

@Component
public class MyBatisInitializer {
    final ApplicationContext applicationContext;

    public MyBatisInitializer(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
        Map<String, DataSource> dsMap = applicationContext.getBeansOfType(DataSource.class);
        DataSource defaultDataSource = applicationContext.getBean(DataSource.class);
        DataPool.getInstance().enrollDataSource(EnvSource.DEFAULT, defaultDataSource);
        dsMap.forEach((k, v) -> {
            if (v != defaultDataSource) DataPool.getInstance().enrollDataSource(k, v);
        });
        DataPool.setMapUnderscoreToCamelCase(true);
    }
}
